package com.he.controller;

import com.he.annotation.AccessLimit;
import com.he.annotation.OperationLogger;
import com.he.domain.Result;
import com.he.domain.vo.SiteConfigVO;
import com.he.entity.SiteConfig;
import com.he.service.SiteConfigService;
import com.he.utils.MinioUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author He
 * @version 1.0
 * @Date 2023/9/14 23:29
 * @Desc 网站配置控制器
 */
@Tag(name = "网站配置控制器")
@RestController
public class SiteConfigController {

    @Resource
    private SiteConfigService siteConfigService;

    @Resource
    private MinioUtils minioUtils;

    @AccessLimit(seconds = 1,maxCount = 10)
    @Operation(description = "获取网站配置")
    @GetMapping("/admin/site")
    public Result<SiteConfigVO> getSiteConfig(){
        return Result.success(siteConfigService.getSiteConfig());
    }

    /**
     * 更新网站配置
     *
     * @param siteConfig 网站配置
     * @return {@link Result<>}
     */
    @OperationLogger(value = "更新网站配置")
    @Operation(description = "更新网站配置")
    @PreAuthorize("@ps.hasPermission('web:site:update')")
    @PutMapping("/admin/site/update")
    public Result<?> updateSiteConfig(@RequestBody SiteConfig siteConfig) {
        System.out.println(siteConfig);
        siteConfigService.updateSiteConfig(siteConfig);
        return Result.success();
    }


    /**
     * 上传网站配置图片
     *
     * @param file 图片
     * @return {@link Result<String>} 图片路径
     */
    @OperationLogger(value = "上传网站配置图片")
    @Operation(description = "上传网站配置图片")
    @PreAuthorize("@ps.hasPermission('web:site:update')")
    @PostMapping("/admin/site/upload")
    public Result<String> uploadSiteImg(@RequestParam("file") MultipartFile file) {
        return Result.success(siteConfigService.uploadSiteImg(file));
    }


}
